<template>
  <van-form @submit="onSubmit">
    <van-field
        border="border"
        v-model="editUser.currentValue"
        :name="editUser.editKey"
        :label="editUser.editName"
        :placeholder="`请输入${editUser.editName}`"
    />
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import {ref} from "vue";
import {updateUser} from "../api/user";

const route = useRoute();

const router = useRouter();


const editUser = ref({
  userId: route.query.userId,
  editKey: route.query.editKey,
  editName: route.query.editName,
  currentValue: route.query.currentValue
})

// 提交
const onSubmit = async () => {
  if (editUser.value.editKey === 'gender') {
    if (editUser.value.currentValue === "男") {
      editUser.value.currentValue = "1";
    } else {
      editUser.value.currentValue = "2";
    }
  }

  const res = await updateUser(editUser.value.userId, editUser.value.editKey, editUser.value.currentValue);
  if (res.data.code === 200) {
    await router.push("/user");
  }
};

</script>

<style scoped>

</style>